package server;

import java.io.IOException;
import java.io.InputStream;

/**
 * 把请求信息封装成Request对象，（根据InputStream输出流封装）
 */
public class Request {
    private String method;
    private String url;
    private String hostAndPort;

    private InputStream inputStream;

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    public Request() {
    }

    public Request(InputStream inputStream) throws IOException {
        this.inputStream = inputStream;
        // 从输入流中获取请求信息
        int count = 0;
        if (count == 0) {
            count = inputStream.available();
        }
        byte[] bytes = new byte[count];
        inputStream.read(bytes);
        String inputStr = new String(bytes);

        String firstLineStr = inputStr.split("\\n")[0];
        String[] str = firstLineStr.split(" ");

        this.method = str[0];
        this.url = str[1];
        this.hostAndPort = inputStr.split("\\n")[1].replace("Host: " , "").replace("\r", "");

        System.out.println("method=========>>>" + method);
        System.out.println("url=========>>>" + url);
    }

    public String getHostAndPort() {
        return hostAndPort;
    }

    public void setHostAndPort(String hostAndPort) {
        this.hostAndPort = hostAndPort;
    }
}
